<?php
/**
 * MVM_MALL 网上商店系统 session文件处理类
 * ============================================================================
 * 版权所有 (C) 2007-2010 www.mvmmall.com，并保留所有权利。
 * 网站地址: http://www.mvmmall.com
 * ----------------------------------------------------------------------------
 * 这是一个免费开源的软件；这意味着您可以在不用于商业目的的前提下对程序代码
 * 进行修改、使用和再发布。
 * ============================================================================
 * $Date: 2008-03-24 $
 * $Id: session.class.php  www.mvmmall.com$
 * ---------------------------------------------
*/
!defined('MVMMALL') && exit('Access Denied');
class mvm_session {
    function handler(){
        session_module_name('user');
        session_set_save_handler(
            array('mvm_session', 'open'),
            array('mvm_session', 'close'),
            array('mvm_session', 'read'),
            array('mvm_session', 'write'),
            array('mvm_session', 'destroy'),
            array('mvm_session', 'gc')
            );
    }

    function make_dir($folder) {
        $reval = false;
        if (!file_exists($folder)) {
            @umask(0);
            preg_match_all('/([^\/]*)\/?/i', $folder, $atmp);
            $base = ($atmp[0][0] == '/') ? '/' : '';
            foreach($atmp[1]AS $val) {
                if ('' != $val) {
                    $base .= $val;

                    if ('..' == $val || '.' == $val) {
                        $base .= '/';

                        continue;
                    }
                } else {
                    continue;
                }

                $base .= '/';

                if (!file_exists($base)) {
                    if (@mkdir($base, 0777)) {
                        @chmod($base, 0777);
                        $reval = true;
                    }
                }
            }
        } else {
            $reval = is_dir($folder);
        }
        clearstatcache();
        return $reval;
    }
    function open($sess_path, $sess_name){
         global $sess_save_path, $sess_save_name, $session_save_dir;
         mvm_session::make_dir($session_save_dir);
        $sess_save_path = realpath($session_save_dir); 
        $sess_save_name = $sess_name;
        return true;
    }
    function close(){
        return true;
    }
    function read($sid){
        global $sess_save_path, $sess_save_name, $sess_data_md5;
        $sess_file = $sess_save_path . '/mvm_sess_' . $sid;
        if(file_exists($sess_file)){
            if(($sess_file_size = filesize($sess_file)) != 0){
                $sess_file_handle = fopen($sess_file, 'r');
                if($sess_file_handle){
                    $sess_data = fread($sess_file_handle, $sess_file_size);
                    $sess_data_md5 = md5($sess_data);
                }
                fclose($sess_file_handle);
            }
        }
        else{
            $sess_data = '';
        }
        Return $sess_data;
    }

    function write($sid, $sess_data){
        global $sess_save_path, $sess_data_md5, $sess_save_name;
        $sess_file = $sess_save_path . '/mvm_sess_' . $sid;
        if($sess_data == ""){
            mvm_session::debug('不需写入', __line__);
            return true;
        }
        if(md5($sess_data) == $sess_data_md5){
            mvm_session::debug('不需更新,数据没发生变化', __line__);
            return touch($sess_file);
        }
        else{
            mvm_session::debug('写入的文件地址：' . $sess_file, __line__);
            $sess_file_handle = fopen($sess_file, 'w');
            if($sess_file_handle){
                return fwrite($sess_file_handle, $sess_data);
                fclose($sess_file_handle);
            }
            else
            {
                return false;
            }
        }
    }
    function destroy($sid){
        global $sess_save_path, $sess_save_name;
        $sess_file = $sess_save_path . '/mvm_sess_' . $sid;
        Return @unlink($sess_file);
    }

    function gc($maxlifetime){
        global $sess_save_path, $session_lifetime;
        $sess_dir_handle = opendir($sess_save_path);
        while(($sess_file = readdir($sess_dir_handle)) !== false){
            $sess_file = $sess_save_path . '/' . $sess_file;
            if(strpos($sess_file, 'mvm_sess_') !== false){
                $sess_mtime = filemtime($sess_file);
                if($sess_mtime + $session_lifetime < time()){
                    @unlink($sess_file);
                }
            }
        }
        Return true;
    }
function debug($msg, $line = null){
        global $session_debug;
        if($session_debug == true){
            printf("行(%s):%s<BR>", $line, $msg);
        }
    }
}